﻿using System;
using System.Configuration;

namespace Higgs.Web.AtomPub.Configurations
{
    public class AtomPubConfigSection : ConfigurationSection
    {
        public static AtomPubConfigSection Current
        {
            get
            {
                return ConfigurationManager.GetSection("atomPubSection") as AtomPubConfigSection;
            }
        }

        [ConfigurationProperty("routePath", IsRequired = false, DefaultValue = "AtomPub/{action}/{id}")]
        public string RoutePath
        {
            get
            {
                var routePath = (string) this["routePath"];
                
                if(routePath.IndexOf("{action}", StringComparison.CurrentCultureIgnoreCase) < 0)
                {
                    if (!routePath.EndsWith("/")) routePath += "/";
                    routePath += "{action}";
                }

                return routePath;
            }
            set { this["routePath"] = value; }
        }

        public string Path
        {
            get
            {
                return RoutePath.RemoveUrlParameter();
            }
        }

        public string AbsolutePath
        {
            get { return Path.ToAbsoluteUrl(); }
        }

        public string ControllerPath
        {
            get { return RoutePath.Substring(0, RoutePath.IndexOf("{action}", StringComparison.CurrentCultureIgnoreCase)); }
        }

        [ConfigurationProperty("workspaceTitle", IsRequired = false, DefaultValue = "AtomPub Site")]
        public string WorkspaceTitle
        {
            get { return (string) this["workspaceTitle"]; }
            set { this["workspaceTitle"] = value; }
        }

        [ConfigurationProperty("feed")]
        public FeedCollectionConfigElement Feed
        {
            get { return (FeedCollectionConfigElement)this["feed"]; }
            set { this["feed"] = value; }
        }

        [ConfigurationProperty("media")]
        public MediaCollectionConfigElement Media
        {
            get { return (MediaCollectionConfigElement)this["media"]; }
            set { this["media"] = value; }
        }
    }
}
